// pages/videoplay/videolist.js
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        barHeight: App.globalData.paddingTop,
        styledata: {
            "bg_color": "white",
            "color": "#000",
            "name": "更多问答",
            "leftflag": true,
            // "homeflag": true,    
        },

        page: 1,
        page_size: 8,

        current_index: 0,
        swiper_list: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getSwiper()
        this.getVideoData()
    },

    getSwiper() {
        let params = {
            type: 'articles',
            // page: 2,
            // limit: 5
        };

        request.post(api.getSwiper, params).then(res => {
            let filterRes = res.filter((item) => {
                if (item.media_type == 'video') return true
            })
            filterRes = filterRes.splice(0, 3)

            this.setData({
                swiper_list: filterRes
            })
            // console.log(this.data.swiper_list)
        })

    },

    // 切换轮播暂停视频播放
    indicator_change(event) {
        // console.log('66666', event);
        // 轮播总长度
        let length = this.data.swiper_list.length
        let newIndex = event.detail.current
        let lastIndex, nextIndex

        // 将正在展示的轮播index，左右视频暂停
        if (newIndex == 0) {
            lastIndex = length - 1
            nextIndex = 1
        } else if (newIndex == length - 1) {
            lastIndex = length - 2
            nextIndex = 0
        } else {
            lastIndex = newIndex - 1
            nextIndex = newIndex + 1
        }

        // this.videoContext = wx.createVideoContext(`myVideo${newIndex}`)
        this.LvideoContext = wx.createVideoContext(`myVideo${lastIndex}`)
        this.NvideoContext = wx.createVideoContext(`myVideo${nextIndex}`)

        this.LvideoContext.pause()
        this.NvideoContext.pause()

    },

    // 获取问答视频数据
    async getVideoData() {

        let params = {
            type: 'articles'
        };

        let res = await request.post(api.getSwiper, params)

        let newres = res.filter(item => {
            if (item.media_type == 'video')
                return true
        });

        // console.log(res, newres);
        this.setData({
            question_list: newres
        })
        // console.log(this.data.swiper_list)
    },

    // 点击刷新
    changeHandle() {
        if (this.data.move == 1) return util.showToast('您操作太快了')
        this.setData({
            move: 1
        })
        setTimeout(async () => {
            await this.getVideoData()

            this.setData({
                move: 0
            })
        }, 1020);
    },

    gotoAnswerVideo(e) {
        let poster = e.currentTarget.dataset.poster
        let text = e.currentTarget.dataset.text
        let url = e.currentTarget.dataset.url
        let id = e.currentTarget.dataset.id


        wx.navigateTo({
            url: `/pages/videoplay/video?url=${url}&text=${text}&poster=${poster}&id=${id}`,
            success: (result) => {

            },
            fail: () => { },
            complete: () => { }
        });

    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})